using dbvc.context.Script;
using dbvc.data;
using dbvc.Queries;
using NUnit.Framework;

namespace tdd.dbvc
{
    [TestFixture]
    public partial class tests
    {
        [TestFixtureSetUp]
        [Category("general")]
        public void SetUp()
        {
            tear_down();
        }
        
        [Test]
        [Category("script")]
        public void add_script()
        {
            root_login();

            new CreateScript(security.Principal, new db_script() { name = "X1", active = true, text = "test1" }).Execute();
            new CreateScript(security.Principal, new db_script() { name = "X2", active = true, text = "test2" }).Execute();
            new CreateScript(security.Principal, new db_script() { name = "X3", active = true, text = "test3" }).Execute();

            Assert.AreEqual(3, GenericQuery<db_script>.Count());
        }

        [Test]
        [Category("script")]
        public void del_script()
        {
            root_login();

            new DeleteScript(security.Principal, GenericQuery<db_script>.GetAll()).Execute();

            Assert.AreEqual(GenericQuery<db_script>.Count(), 0);
        }

        [Test]
        [Category("script")]
        public void check_attach_to_version()
        {
            root_login();

            add_version();

            add_script();

            detach_all();

            var ver = GenericQuery<db_version>.GetAll()[0];
            var srs = GenericQuery<db_script>.GetAll();

            new AttachScript(security.Principal, ver, srs).Execute();

            Assert.AreEqual(3, GenericQuery<db_version_script>.Count());

            detach_all();

            new AttachScript(security.Principal, ver, srs[0]).Execute();

            Assert.AreEqual(1, GenericQuery<db_version_script>.Count());

        }

        [Test]
        [Category("script")]
        public void detach_all()
        {
            GenericQuery<db_version_script>.DeleteAll();

            Assert.AreEqual(0, GenericQuery<db_version_script>.Count());
        }
    }
}